package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.SettleMapper;
import com.hqyj.pojo.Settle;
import com.hqyj.service.SettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;


@Service
public class SettleServiceImpl implements SettleService {

    //注入mapper
    @Autowired(required = false)
    SettleMapper settleMapper;

    @Override
    public HashMap<String, Object> select(Settle s) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<Settle> page = new Page<>(s.getPage(),s.getRow());

        //条件构造器
        QueryWrapper<Settle> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(s.getSettleId()!=null,"settle_id",s.getSettleId());
        queryWrapper.like(s.getSettleIncome()!=null&&!s.getSettleIncome().equals(""),"settle_income",s.getSettleIncome());

            //查询数据库
        Page<Settle> sList =settleMapper.selectPage(page,queryWrapper);
            //当前页集合
        map.put("list",sList.getRecords());
            //总页数
        map.put("totalPage",sList.getPages());
            //总条数
        map.put("total",sList.getTotal());
            //当前页
        map.put("curPage",s.getPage());
        if(s.getPage()==1){
                //上一页
                map.put("prePage",1);

        }else{
                map.put("prePage",s.getPage()-1);

        }
        if(s.getPage()==sList.getPages()){
                //下一页
                map.put("nextPage",sList.getPages());

        }else{
                map.put("nextPage",s.getPage()+1);

        }
            //每页显示条数
          map.put("row",s.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> add(Settle s) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = settleMapper.insert(s);
        if(num>0){
                map.put("info","保存成功");
        }else{
                map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Settle s) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        //修改
        int num = settleMapper.updateById(s);
        if(num>0){
                map.put("info","保存成功");
        }else{
                map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer s) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num = settleMapper.deleteById(s);
        if(num>0){
            map.put("info","删除成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }


}
